import 'package:flutter/material.dart';
import 'package:ls_work/a_yyt/page/policy_record_list_page.dart';
import 'package:ls_work/res/colors.dart';
import 'package:ls_work/res/styles.dart';

class PolicyRecordPage extends StatefulWidget {
  @override
  _PolicyRecordPageState createState() => _PolicyRecordPageState();
}

class _PolicyRecordPageState extends State<PolicyRecordPage>
    with TickerProviderStateMixin, AutomaticKeepAliveClientMixin {
  PageController _pageController = PageController(initialPage: 0);
  TabController? _tabController;
  // int _pageIndex = 0;

  @override
  bool get wantKeepAlive => true;

  @override
  void initState() {
    super.initState();
    _tabController = new TabController(vsync: this, length: 5);
  }

  @override
  void dispose() {
    _tabController?.dispose();
    _pageController.dispose();
    super.dispose();
  }

  _onPageChange(int index) {
    // _pageIndex = index;
    _tabController?.animateTo(index);
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return Scaffold(
      appBar: AppBar(
        elevation: 0.5,
        title: Text('报警记录'),
        bottom: TabBar(
          onTap: (index) {
            if (!mounted) {
              return;
            }
            _pageController.jumpToPage(index);
          },
          isScrollable: true,
          controller: _tabController,
          labelStyle: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
          indicatorSize: TabBarIndicatorSize.label,
          unselectedLabelColor: Colours.text_gray,
          unselectedLabelStyle: TextStyles.textGray16,
          labelColor: Colours.text_dark,
          indicatorColor: Colours.app_main,
          indicatorPadding: EdgeInsets.only(bottom: 5),
          tabs: <Widget>[
            Tab(
              text: '今天',
            ),
            Tab(
              text: '昨天',
            ),
            Tab(
              text: '近7天',
            ),
            Tab(
              text: '近14天',
            ),
            Tab(
              text: '近30天',
            ),
          ],
        ),
      ),
      body: PageView.builder(
        key: const Key('pageView'),
        itemCount: 5,
        onPageChanged: _onPageChange,
        controller: _pageController,
        itemBuilder: (BuildContext context, int index) {
          int queryTimeType = 1;
          switch (index) {
            case 0:
              queryTimeType = 1;
              break;
            case 1:
              queryTimeType = 0;
              break;
            case 2:
              queryTimeType = 2;
              break;
            case 3:
              queryTimeType = 3;
              break;
            case 4:
              queryTimeType = 4;
              break;
            default:
              queryTimeType = 1;
          }
          return PolicyRecordListPage(queryType: queryTimeType);
        },
      ),
    );
  }
}
